#!/usr/bin/env python3

from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration

def generate_launch_description():
    # 声明启动参数
    topic_name_arg = DeclareLaunchArgument(
        'topic_name',
        default_value='custom_topic',
        description='Custom topic name for remapping'
    )

    # 创建带重映射的节点
    publisher_node = Node(
        package='multi_launch_demo',
        executable='publisher_node',
        name='publisher_node',
        parameters=[{
            'publish_frequency': 1.0,
            'message_prefix': 'Remapped'
        }],
        remappings=[
            ('topic', LaunchConfiguration('topic_name'))
        ],
        output='screen'
    )

    subscriber_node = Node(
        package='multi_launch_demo',
        executable='subscriber_node',
        name='subscriber_node',
        remappings=[
            ('topic', LaunchConfiguration('topic_name'))
        ],
        output='screen'
    )

    # 创建另一个发布者，发布到不同的主题
    publisher_node2 = Node(
        package='multi_launch_demo',
        executable='publisher_node',
        name='publisher_node2',
        parameters=[{
            'publish_frequency': 0.5,
            'message_prefix': 'Second Publisher'
        }],
        remappings=[
            ('topic', 'second_topic')
        ],
        output='screen'
    )

    # 创建订阅者，订阅第二个主题
    subscriber_node2 = Node(
        package='multi_launch_demo',
        executable='subscriber_node',
        name='subscriber_node2',
        remappings=[
            ('topic', 'second_topic')
        ],
        output='screen'
    )

    return LaunchDescription([
        topic_name_arg,
        publisher_node,
        subscriber_node,
        publisher_node2,
        subscriber_node2
    ])
